//
//  LCBaoTableViewCell.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/11.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCBaoTableViewCell.h"
#import "LLImagePickerView.h"
#import "CommitModel.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "LCAudioViewController.h"
#import "GenderPickerView.h"
#import "PropertyListModel.h"
#import "CountHeight.h"
@interface LCBaoTableViewCell ()<UIActionSheetDelegate>
@property (strong,nonatomic)CommitModel *commitModel;
@property (retain,nonatomic) NSMutableArray *selectedAssets;
@property (weak, nonatomic) IBOutlet UIButton *chooseBtn;
@property (retain,nonatomic) NSMutableArray *selectedPhotos;
@property (copy,nonatomic)NSString           *typeStr;
@property (nonatomic,strong)NSArray          *typeListArr;
@end

@implementation LCBaoTableViewCell
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)awakeFromNib {
    [super awakeFromNib];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(receiveNotificiation:) name:@"AudioData" object:nil];
    _typeStr = @"物业";
    _commitModel = [[CommitModel alloc]init];
    _commitModel.message_obj = @"obj_type_03";
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.bottomView.layer.borderWidth = 1;
    self.bottomView.layer.borderColor = [UIColor colorWithHex:0xb8bcbf].CGColor;
    self.commitBtn.layer.cornerRadius = 3;
    self.commitBtn.layer.masksToBounds = YES;
    [_nameTextFiled addTarget:self action:@selector(nameChange:) forControlEvents:UIControlEventEditingChanged];
    [_phoneTextField addTarget:self action:@selector(phoneChange:) forControlEvents:UIControlEventEditingChanged];
    [_messageText addTarget:self action:@selector(messageChange:) forControlEvents:UIControlEventEditingChanged];
    LLImagePickerView *pickerV = [[LLImagePickerView alloc]initWithFrame:CGRectMake(10+10+28, 270, SCREEN_WIDTH - 2*17-10-28-10-20, 75)];
    [_bottomView addSubview:pickerV];
    pickerV.type = LLImageTypePhoto;
    pickerV.maxImageSelected = 5;
    pickerV.allowMultipleSelection = NO;
//    pickerV.showDelete = NO;
    pickerV.allowPickingVideo = YES;
    [pickerV observeSelectedMediaArray:^(NSArray<LLImagePickerModel *> *list)
    {
        for (LLImagePickerModel *model in list)
        {
            if (!model.isVideo)
            {
               //不是视频
                [_commitModel.imgArr addObject:model.image];
            }
            else
            {
                PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
                options.version = PHImageRequestOptionsVersionCurrent;
                options.deliveryMode = PHVideoRequestOptionsDeliveryModeAutomatic;
                PHImageManager *manager = [PHImageManager defaultManager];
                [manager requestAVAssetForVideo:model.asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info)
                 {
                     AVURLAsset *urlAsset = (AVURLAsset *)asset;
                     NSURL *url = urlAsset.URL;
                     NSData *videoData = [NSData dataWithContentsOfURL:url];
                     //视频
                     [_commitModel.videoArr addObject:videoData];
                 }];
            }
        }
    }];
    NSMutableDictionary *typeDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    if (get_sp(@"company_id"))
    {
        [typeDic setObject:get_sp(@"company_id") forKey:@"company_id"];
        [typeDic setObject:@"100" forKey:@"pageSize"];
        [typeDic setObject:@"1" forKey:@"pageNum"];
    }
    
    [MDataProvider socialWorkerListBodyDic:typeDic sucess:^(NSArray *array) {
        _typeListArr = [NSArray arrayWithArray:array];
        
    } faile:^(NSString *str) {
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
- (void)nameChange:(UITextField *)sender
{
    if (sender.text.length>0)
    {
        _commitModel.name = sender.text;
    }
}
- (void)phoneChange:(UITextField *)sender
{
    if (sender.text.length>0)
    {
        _commitModel.phone = sender.text;
    }
}
- (void)messageChange:(UITextField *)sender
{
    if (sender.text.length>0)
    {
      _commitModel.message_content = sender.text;
    }
}
- (IBAction)commitBtnClick:(UIButton *)sender
{
    if (_commitModel.phone.length>0)
    {
        //当手机号长度大于0的时候检验手机号
        if ([CountHeight validateMobile:_commitModel.phone])
        {
            [SVProgressHUD showInfoWithStatus:@"请输入正确手机号!"];
            return;
        }
    }
    if ([_commitModel.message_obj isEqualToString:@"obj_type_03"])
    {
        //物业
        if (_commitModel.messageobjidstr.length == 0)
        {
            [SVProgressHUD showInfoWithStatus:@"请选择服务对象!"];
            return;
        }
    }
    if (_commitModel.message_content.length == 0)
    {
        [SVProgressHUD showInfoWithStatus:@"留言不能为空!"];
         return;
    }
    else if(_commitModel.message_content.length > 30)
    {
        [SVProgressHUD showInfoWithStatus:@"留言内容请控制在30个字以内!"];
        return;
    }
    if ([_commitModel.file_function isEqualToString:@"message"])
    {
        //留言
        [MDataProvider leaveMessageBodyDic:_commitModel sucess:^(NSString *success)
        {
            [SVProgressHUD showSuccessWithStatus:success];
        } faile:^(NSString *str)
        {
             [SVProgressHUD showInfoWithStatus:str];
        }];
    }
    else
    {
        //保修
        [MDataProvider warrantyBodyDic:_commitModel sucess:^(NSString *success) {
            [SVProgressHUD showSuccessWithStatus:success];
        } faile:^(NSString *str) {
            [SVProgressHUD showInfoWithStatus:str];
        }];
    }
}
//通知中心的回调方法
- (void)receiveNotificiation:(NSNotification*)sender{
    if (sender.userInfo[@"audio"])
    {
        [_commitModel.audeoArr addObject:sender.userInfo[@"audio"]];
    }
}
- (IBAction)AudioBtnClick:(UIButton *)sender
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    LCAudioViewController *vc = [[LCAudioViewController alloc]init];
    [window.rootViewController presentViewController:vc animated:YES completion:nil];
}
//选择社工和物业
- (IBAction)chooseBtnClick:(UIButton *)sender
{
    if (sender.tag == 1)
    {
        _typeStr = @"物业";
        _socalImg.image = img(@"lc_type.png");
        _propertyImg.image = img(@"lc_notype.png");
        _commitModel.message_obj = @"obj_type_03";
    }
    else
    {
        _typeStr = @"社工";
        _propertyImg.image = img(@"lc_type.png");
        _socalImg.image = img(@"lc_notype.png");
         _commitModel.message_obj = @"obj_type_02";
    }
}
//选择服务对象
- (IBAction)serviceBtnClick:(UIButton *)sender
{
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    if([_typeStr isEqualToString:@"社工"])
    {
        ALERT_SHOW(@"社工不需要选择服务对象");
        return;
    }
    //弹出物业公司
    GenderPickerView *view = [[GenderPickerView alloc]initWithArr:_typeListArr];
    __weak typeof(self) weak = self;
    view.block = ^(NSString *gender)
    {
        for (PropertyListModel *model in _typeListArr)
        {
            if ([gender isEqualToString:model.property_name])
            {
                weak.commitModel.messageobjidstr = model.property_id;
            }
            weak.typeTextFile.text = gender;
        }
    };
    [window addSubview:view];
    [view showBottomView];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    //调用dataSource
    if (self.dataSource && [self.dataSource respondsToSelector:@selector(chooseTypeBtnClick)])
    {
        _commitModel.file_function = [self.dataSource chooseTypeBtnClick];
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
@end
